layui.use(['form','layer','element'],function(){
	var form = layui.form,layer=layui.layer,element = layui.element;
	
	
	var form = layui.form,layer=layui.layer,element=layui.element;
	
	$("#save-btn").on('click',function(){save();});
	
	$("#datasetName").on("change",function(e){
		var value = $(this).val();
		$("#datasetCode").val("DS-"+pinyinUtil.getFirstLetter(value));
	});
	
	var content = '<div class="layui-tab-item layui-show" name="tab-content">'+
							 '<div class="layui-form-item">'+
							 '<label for="paraData" class="layui-form-label" >重复使用:</label>'+
									 '<div class="layui-input-inline">'+
										'<div class="layui-input-inline">'+
										   '<select name="reuseType">'+
										       '<option value="1" selected>是</option>'+
											   '<option value="2" >否</option>'+
										   '</select>'+
										  
										'</div>'+
									'</div>'+
							 '</div>'+
						     '<div class="layui-form-item" id="cloneObj">'+
									'<label for="paraData" class="layui-form-label" >参数值:</label>'+
									 '<div class="layui-input-inline">'+
										'<input type="text" name="paraData" autocomplete="off" class="layui-input" maxlength="30">'+
									'</div>'+
									'<div class="layui-form-mid layui-word-aux">'+
										 '<i class="layui-icon" style="cursor:pointer;" onclick="addParaVal(this)">&#xe654;</i>'+
									'</div>'+   
								'</div>'+
						    '</div>';
	function add(){
		layer.prompt({title: '输入变量名，并确认', formType: 2}, function(text, index){
			
				var isvalidated = true;
				$("span[name='title']").each(function(){
					var paraName = $(this).text();
					if(paraName == text){
						layer.alert("变量名已存在",{
							icon:5,
							title:"错误"
						});
						isvalidated = false;
						return false;
					}
				});
				if(!isvalidated)
					return false;
				layer.close(index);
				var id = new Date().getTime();
				element.tabAdd('demo', {
					title: '<span name="title">'+text+'</span>',
					content:content,
					id:id
					})
				form.render('select');
				element.tabChange('demo', id);
			});
		}
	window.add = add;
	
	function addParaVal(obj){
		var cloneObj = $("#cloneObj").clone();
		cloneObj.removeAttr("id");
		cloneObj.find("input[name='paraData']").val("");
		$(obj).parent("div").parent("div").parent("div").append(cloneObj);
		cloneObj.find("div:eq(1)").append('<i class="layui-icon" style="cursor:pointer" onclick="delParaVal(this)">&#x1006;</i>');
		
	}
	window.addParaVal = addParaVal;
	
	function delParaVal(obj){
		$(obj).parent("div").parent("div").remove();
	}
	
	window.delParaVal = delParaVal;
	
	
	function isBlank(val){
		
		if(typeof val == "undefined" || val == "")
			return true;
		return false;
		
	}
	
	function save(){
		
		var data = {};
		var datasetName = $("#datasetName").val();
		if(isBlank(datasetName)){
			layer.tips("请填写名称","datasetName",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}
		data['datasetName'] = datasetName;
		
		var datasetCode = $("#datasetCode").val();
		if(isBlank(datasetCode)){
			layer.tips("请填写编码","datasetCode",{
      			  tips: [1, '#FF0000'],
      			  time: 3000
      			});
			return false;
		}
		data['datasetCode'] = datasetCode;
		
		var reuseType = $("#datasetReuseType").val();
		data['reuseType'] = reuseType;
		
		var datasetType = $("#datasetType").val();
		data['datasetType'] = datasetType;
		
		var datasetStatus = $("#datasetStatus").val();
		data['datasetStatus'] = datasetStatus;
		
		var isValidated = true;
		var para = [];
		$("#para>li").each(function(index){
			
			var paraName = $(this).find("span").text();
			
			var paraDataList = [];
			
			var contentDiv = $("div[name='tab-content']:eq("+index+")");
			
			var paraReuseType = contentDiv.find("select[name='reuseType']").val();
			
			contentDiv.find("input[name='paraData']").each(function(index1){
				var paraDataVal = $(this).val();
				if(isBlank(paraDataVal)){
					isValidated = false;
					layer.tips("请填写参数值",$(this),{
						tips: [1, '#FF0000'],
						time: 3000
					});
					return false;
				}
				paraDataList.push({'paraData':$(this).val(),'dataOrder':index1+1});
			});
				
			para.push({'paraName':paraName,'reuseType':paraReuseType,'paraDataList':paraDataList});
		});
		
		if(!isValidated)
			return false;
		
		data['paraList'] = para;
		ajaxPost('../dataset/saveDataset',{"str":JSON.stringify(data)},
				function(){
					 var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		    		 parent.layer.close(index);
		    		 parent.initPage();
				},
				function(data){
					  layer.alert(data.errorMsg,{
							icon:5,
							title:"错误"
						});
				  });
		//console.log(JSON.stringify(data));
	}
});